/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

/**
 *
 * @author phong
 */
public class ComplexType extends Type{

    Type first, second;

    public ComplexType(Type first, Type second) {
        this.first = first;
        this.second = second;
    }

    public Type getFirst() {
        return first;
    }

    public Type getSecond() {
        return second;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof ComplexType) {
            ComplexType other = (ComplexType)o;
            return first.equals(other.first) && second.equals(other.second);
        }
        else
            return false;
    }

    @Override
    public boolean matches(Type other) {
        if (other == UNKNOWN_TYPE)
            return true;
        if (other instanceof ComplexType) {
            ComplexType t = (ComplexType)other;
            return first.matches(t.first) && second.matches(t.second);
        }
        else
            return false;
    }

    @Override
    public Type resolve(Type other) {
        if (other == UNKNOWN_TYPE)
            return this;
        else if (other instanceof ComplexType) {
            ComplexType ct = (ComplexType)other;
            Type f = first.resolve(ct.first);
            Type s = second.resolve(ct.second);
            if (f != null && s != null)
                return new ComplexType(f, s);
            else
                return null;
        }
        else
            return null;
    }

    @Override
    public String toString() {
        return "(" + first.toString() + "," + second.toString() + ")";
    }

    @Override
    public String getName() {
        return "(" + first.getName() + " -> " + second.getName() + ")";
    }
}
